package com.fieldchain.ex.fa100.ui.personal.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.utils.common.VerticalUtils;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.common.EmailCode;
import com.fieldchain.ex.fa100.utils.net.modle.ImageRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/15
 */
public class ChangeEmailctivity extends MyBaseActivity implements View.OnClickListener{
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TextView tvEmail;
    private EditText etImgCode;
    private ImageView ivImgcode;
    private EditText etSmsCode;
    private TextView tvSms;
    private TextView tvSure;

    private String email="";
    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        tvEmail = findViewById(R.id.tv_email);
        etImgCode = findViewById(R.id.et_imgCode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etSmsCode = findViewById(R.id.et_smsCode);
        tvSms = findViewById(R.id.tv_sms);
        tvSure = findViewById(R.id.tv_sure);
    }

    @Override
    public void setValue() {
        getImage();
        email= ShareManager.getInstance().getUser().getEmail();
        tvEmail.setText(email);
        tvCenter.setText("修改邮箱");
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        ivImgcode.setOnClickListener(this);
        tvSms.setOnClickListener(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        VerticalUtils.getInstance().cancle();
    }
    @Override
    public int getLayout() {
        return R.layout.activity_changeemail;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_imgcode:
                getImage();
                break;
            case R.id.tv_sms:
                VerticalUtils.getInstance().start(tvSms);
                EmailCode.getInstance().getEmail(tvEmail.getText().toString(), etImgCode.getText().toString());
                break;
            case R.id.tv_sure:
                send();
                break;
            case R.id.rea_back:
                finish();
                break;
        }
    }

    public void getImage() {
        HttpRequest.getInstance().getImage(Constant.image, new ImageRequest() {
            @Override
            public void imgReceive(Bitmap bit) {
                ivImgcode.setImageBitmap(bit);
            }
        });
    }

    public void send(){
        String sms=etSmsCode.getText().toString();
        if(sms.length()==0){
            MyToast.show("请输入邮箱验证码");
            return;
        }
        HashMap map=new HashMap();
//        map.put("email",email);
        map.put("oldCode",sms);
        map.put("step",1+"");
        HttpRequest.getInstance().postRequest(map, Constant.changeEmail, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                if(code==200){
                    startActivity(new Intent(mContext,ChangeEmailNext.class));
                    finish();
                }
                MyToast.show(msg);
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
